package com.dz.finance.api.controller;

import com.dz.finance.biz.dto.ClientDTO;
import com.dz.finance.biz.dto.PhonePasswordLoginDTO;
import com.dz.finance.biz.dto.PhoneSmsLoginDTO;
import com.dz.finance.biz.dto.SmsCodeDTO;
import com.dz.finance.biz.service.MemberLoginService;
import com.dz.finance.common.entity.Token;
import com.dz.finance.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author: DengHaoQi
 * @date: 2024/5/22 16:52
 * @description:
 */

@Api(tags = "用户登陆模块")
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/login")
public class LoginController {

    private final MemberLoginService memberLoginService;

    @ApiOperation("获取客户端id")
    @GetMapping("/getClientId")
    public Result<String> getClientId() {
        String clientId = memberLoginService.getClientId();
        return Result.ok(clientId);
    }


    @ApiOperation("获取图形验证码")
    @GetMapping("/getBase64Code")
    public Result<String> getBase64Code(@Validated @ModelAttribute ClientDTO clientDTO) {
        String clientId = clientDTO.getClientId();
        String code = memberLoginService.getBase64Code(clientId);
        return Result.ok(code);
    }


    @ApiOperation("获取短信验证码")
    @GetMapping("/sendSmsCode")
    public Result<Void> sendSmsCode(@Validated SmsCodeDTO smsCodeDTO) {
        memberLoginService.sendSmsCode(smsCodeDTO);
        return Result.ok();
    }


    @ApiOperation("手机号密码登陆")
    @PostMapping("/phonePasswordLogin")
    public Result<Token> loginByPhonePassword(@RequestBody @Validated PhonePasswordLoginDTO phonePasswordLoginDTO) {
        Token token = memberLoginService.loginByPhonePassword(phonePasswordLoginDTO);
        return Result.ok(token);
    }


    @ApiOperation("手机号短信验证码登陆")
    @PostMapping("/phoneSmsCodeLogin")
    public Result<Token> loginByPhoneSms(@RequestBody @Validated PhoneSmsLoginDTO phoneSmsLoginDTO) {
        Token token = memberLoginService.loginByPhoneSms(phoneSmsLoginDTO);
        return Result.ok(token);
    }


    @ApiOperation("轮询检测微信扫码登陆状态")
    @GetMapping(value = "/getClientToken")
    public Result<Token> polling(@Validated @ModelAttribute ClientDTO clientDTO) {
        Token token = memberLoginService.getClientToken(clientDTO.getClientId());
        return Result.ok(token);
    }

}
